import java.io.*;
import java.net.*;
import java.util.*;

	/**
	* @author Tom Naps
	* @author Joshua Oberle.
	*/

class LogServer {

    private static Vector clientList = new Vector (4,4);
    private static int id = 0;
  
	/**
	* This starts the server at a specified port.
	*
	* @param args[0] If no port is provided 8190 is used.
	*/

    public static void main (String [] args) throws IOException {
    
	//Get the port and created a socket there.
	int port = 8190;
	if (args.length > 0)
	    port = Integer.parseInt(args[0]);
	ServerSocket listener = new ServerSocket(port);
	//System.out.println("The Chat Server is running on port " + port);

	// Listen for clients. Start a new handler for each.
	// Add each client to the linked list.
	while (true) {
	    Socket client = listener.accept();
	    new LogBoss(client).start();
	    //System.out.println("New client no."+id + " on client's port "+client.getPort());
	    clientList.addElement(client);
	    id++;
	}
    }

	/**
	* Broadcasts a message.
	*
	* @param message message to be broadcast.
	* @param args[1] name of connection to send over.
	*/

    static synchronized void broadcast (String message, String name) 
	throws IOException {
    
	// Sends the message to every client including the sender.
	Socket s;
	PrintWriter p;
	for (int k = 0; k < clientList.size(); k++) {
	    s = (Socket) clientList.elementAt(k);
	    p = new PrintWriter(s.getOutputStream(), true);
	    p.println(name + ": "+message);
	}
    }

	/**
	* Removes from the socket from the server.
	*
	* @param s The Socket to be removed.
	*/

    static synchronized void remove (Socket s) {

	// Finds the client on the list (by comparing socket
	// references) and removes it.
	int k;
	Socket t;
	for (k = 0; k < clientList.size(); k++) {
	    t = (Socket) clientList.elementAt(k);
	    if (t.equals(s)) break;
	}
	clientList.removeElementAt(k);
	id--;
    }    
}

	/**
	* Handles the log files.
	*
	* @author Tom Naps
	* @author Joshua Oberle
	*/

class LogBoss extends Thread {
  
    // The Chat Handler class is called from the Chat Server:
    // one thread for each client coming in to chat.

    private BufferedReader in;
    private PrintWriter out;
    private Socket toClient;
    private String name;
    private LogLineReader llr;

	/**
	* The LogBoss is controling the specified client.
	*
	* @param s The Socket to give to this LogBoss.
	*/

    LogBoss (Socket s) {
	toClient = s;
    }

	/**
	* Runs the LogBoss.
	*
	*/

    public void run () {
			llr = new LogLineReader();
	try {

	    // Create i-o streams through the socket we were
	    // given when the thread was instantiated
	    // and welcome the new client.
	    out = new PrintWriter(toClient.getOutputStream(), true);
	    in = new BufferedReader 
		(new InputStreamReader ( (toClient.getInputStream())));
 	    String name = toClient.getInetAddress().toString();
     
	    // Read lines and send them off for broadcasting.
	    String s = "";
	    while (true) {
		s = in.readLine();
		if(s == null)
			break;
		llr.nextLine(s);
	    }
	    LogServer.remove(toClient);
	    toClient.close();
	    llr.writeToDatabase();
	}
	catch (Exception e) {
		e.printStackTrace();
	}
    }
}